<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>dom.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_editor_dom.js.html">dom.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Testing utilities for editor specific DOM related tests.
<a name="line17"></a> *
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>goog.provide(&#39;goog.testing.editor.dom&#39;);
<a name="line21"></a>
<a name="line22"></a>goog.require(&#39;goog.dom.NodeType&#39;);
<a name="line23"></a>goog.require(&#39;goog.dom.TagIterator&#39;);
<a name="line24"></a>goog.require(&#39;goog.dom.TagWalkType&#39;);
<a name="line25"></a>goog.require(&#39;goog.iter&#39;);
<a name="line26"></a>goog.require(&#39;goog.string&#39;);
<a name="line27"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line28"></a>
<a name="line29"></a>
<a name="line30"></a>/**
<a name="line31"></a> * Returns the previous (in document order) node from the given node that is a
<a name="line32"></a> * non-empty text node, or null if none is found or opt_stopAt is not an
<a name="line33"></a> * ancestor of node. Note that if the given node has children, the search will
<a name="line34"></a> * start from the end tag of the node, meaning all its descendants will be
<a name="line35"></a> * included in the search, unless opt_skipDescendants is true.
<a name="line36"></a> * @param {Node} node Node to start searching from.
<a name="line37"></a> * @param {Node=} opt_stopAt Node to stop searching at (search will be
<a name="line38"></a> *     restricted to this node&#39;s subtree), defaults to the body of the document
<a name="line39"></a> *     containing node.
<a name="line40"></a> * @param {boolean=} opt_skipDescendants Whether to skip searching the given
<a name="line41"></a> *     node&#39;s descentants.
<a name="line42"></a> * @return {Text} The previous (in document order) node from the given node
<a name="line43"></a> *     that is a non-empty text node, or null if none is found.
<a name="line44"></a> */
<a name="line45"></a>goog.testing.editor.dom.getPreviousNonEmptyTextNode = function(
<a name="line46"></a>    node, opt_stopAt, opt_skipDescendants) {
<a name="line47"></a>  return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
<a name="line48"></a>      node, opt_stopAt, opt_skipDescendants, true);
<a name="line49"></a>};
<a name="line50"></a>
<a name="line51"></a>
<a name="line52"></a>/**
<a name="line53"></a> * Returns the next (in document order) node from the given node that is a
<a name="line54"></a> * non-empty text node, or null if none is found or opt_stopAt is not an
<a name="line55"></a> * ancestor of node. Note that if the given node has children, the search will
<a name="line56"></a> * start from the start tag of the node, meaning all its descendants will be
<a name="line57"></a> * included in the search, unless opt_skipDescendants is true.
<a name="line58"></a> * @param {Node} node Node to start searching from.
<a name="line59"></a> * @param {Node=} opt_stopAt Node to stop searching at (search will be
<a name="line60"></a> *     restricted to this node&#39;s subtree), defaults to the body of the document
<a name="line61"></a> *     containing node.
<a name="line62"></a> * @param {boolean=} opt_skipDescendants Whether to skip searching the given
<a name="line63"></a> *     node&#39;s descentants.
<a name="line64"></a> * @return {Text} The next (in document order) node from the given node that
<a name="line65"></a> *     is a non-empty text node, or null if none is found or opt_stopAt is not
<a name="line66"></a> *     an ancestor of node.
<a name="line67"></a> */
<a name="line68"></a>goog.testing.editor.dom.getNextNonEmptyTextNode = function(
<a name="line69"></a>    node, opt_stopAt, opt_skipDescendants) {
<a name="line70"></a>  return goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_(
<a name="line71"></a>      node, opt_stopAt, opt_skipDescendants, false);
<a name="line72"></a>};
<a name="line73"></a>
<a name="line74"></a>
<a name="line75"></a>/**
<a name="line76"></a> * Helper that returns the previous or next (in document order) node from the
<a name="line77"></a> * given node that is a non-empty text node, or null if none is found or
<a name="line78"></a> * opt_stopAt is not an ancestor of node. Note that if the given node has
<a name="line79"></a> * children, the search will start from the end or start tag of the node
<a name="line80"></a> * (depending on whether it&#39;s searching for the previous or next node), meaning
<a name="line81"></a> * all its descendants will be included in the search, unless
<a name="line82"></a> * opt_skipDescendants is true.
<a name="line83"></a> * @param {Node} node Node to start searching from.
<a name="line84"></a> * @param {Node=} opt_stopAt Node to stop searching at (search will be
<a name="line85"></a> *     restricted to this node&#39;s subtree), defaults to the body of the document
<a name="line86"></a> *     containing node.
<a name="line87"></a> * @param {boolean=} opt_skipDescendants Whether to skip searching the given
<a name="line88"></a> *   node&#39;s descentants.
<a name="line89"></a> * @param {boolean=} opt_isPrevious Whether to search for the previous non-empty
<a name="line90"></a> *     text node instead of the next one.
<a name="line91"></a> * @return {Text} The next (in document order) node from the given node that
<a name="line92"></a> *     is a non-empty text node, or null if none is found or opt_stopAt is not
<a name="line93"></a> *     an ancestor of node.
<a name="line94"></a> * @private
<a name="line95"></a> */
<a name="line96"></a>goog.testing.editor.dom.getPreviousNextNonEmptyTextNodeHelper_ = function(
<a name="line97"></a>    node, opt_stopAt, opt_skipDescendants, opt_isPrevious) {
<a name="line98"></a>  opt_stopAt = opt_stopAt || node.ownerDocument.body;
<a name="line99"></a>  // Initializing the iterator to iterate over the children of opt_stopAt
<a name="line100"></a>  // makes it stop only when it finishes iterating through all of that
<a name="line101"></a>  // node&#39;s children, even though we will start at a different node and exit
<a name="line102"></a>  // that starting node&#39;s subtree in the process.
<a name="line103"></a>  var iter = new goog.dom.TagIterator(opt_stopAt, opt_isPrevious);
<a name="line104"></a>
<a name="line105"></a>  // TODO(user): Move this logic to a new method in TagIterator such as
<a name="line106"></a>  // skipToNode().
<a name="line107"></a>  // Then we set the iterator to start at the given start node, not opt_stopAt.
<a name="line108"></a>  var walkType; // Let TagIterator set the initial walk type by default.
<a name="line109"></a>  var depth = goog.testing.editor.dom.getRelativeDepth_(node, opt_stopAt);
<a name="line110"></a>  if (depth == -1) {
<a name="line111"></a>    return null; // Fail because opt_stopAt is not an ancestor of node.
<a name="line112"></a>  }
<a name="line113"></a>  if (node.nodeType == goog.dom.NodeType.ELEMENT) {
<a name="line114"></a>    if (opt_skipDescendants) {
<a name="line115"></a>      // Specifically set the initial walk type so that we skip the descendant
<a name="line116"></a>      // subtree by starting at the start if going backwards or at the end if
<a name="line117"></a>      // going forwards.
<a name="line118"></a>      walkType = opt_isPrevious ? goog.dom.TagWalkType.START_TAG :
<a name="line119"></a>                                  goog.dom.TagWalkType.END_TAG;
<a name="line120"></a>    } else {
<a name="line121"></a>      // We&#39;re starting &quot;inside&quot; an element node so the depth needs to be one
<a name="line122"></a>      // deeper than the node&#39;s actual depth. That&#39;s how TagIterator works!
<a name="line123"></a>      depth++;
<a name="line124"></a>    }
<a name="line125"></a>  }
<a name="line126"></a>  iter.setPosition(node, walkType, depth);
<a name="line127"></a>
<a name="line128"></a>  // Advance the iterator so it skips the start node.
<a name="line129"></a>  try {
<a name="line130"></a>    iter.next();
<a name="line131"></a>  } catch (e) {
<a name="line132"></a>    return null; // It could have been a leaf node.
<a name="line133"></a>  }
<a name="line134"></a>  // Now just get the first non-empty text node the iterator finds.
<a name="line135"></a>  var filter = goog.iter.filter(iter,
<a name="line136"></a>                                goog.testing.editor.dom.isNonEmptyTextNode_);
<a name="line137"></a>  try {
<a name="line138"></a>    return /** @type {Text} */ (filter.next());
<a name="line139"></a>  } catch (e) { // No next item is available so return null.
<a name="line140"></a>    return null;
<a name="line141"></a>  }
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Returns whether the given node is a non-empty text node.
<a name="line147"></a> * @param {Node} node Node to be checked.
<a name="line148"></a> * @return {boolean} Whether the given node is a non-empty text node.
<a name="line149"></a> * @private
<a name="line150"></a> */
<a name="line151"></a>goog.testing.editor.dom.isNonEmptyTextNode_ = function(node) {
<a name="line152"></a>  return !!node &amp;&amp; node.nodeType == goog.dom.NodeType.TEXT &amp;&amp; node.length &gt; 0;
<a name="line153"></a>};
<a name="line154"></a>
<a name="line155"></a>
<a name="line156"></a>/**
<a name="line157"></a> * Returns the depth of the given node relative to the given parent node, or -1
<a name="line158"></a> * if the given node is not a descendant of the given parent node. E.g. if
<a name="line159"></a> * node == parentNode returns 0, if node.parentNode == parentNode returns 1,
<a name="line160"></a> * etc.
<a name="line161"></a> * @param {Node} node Node whose depth to get.
<a name="line162"></a> * @param {Node} parentNode Node relative to which the depth should be
<a name="line163"></a> *     calculated.
<a name="line164"></a> * @return {number} The depth of the given node relative to the given parent
<a name="line165"></a> *     node, or -1 if the given node is not a descendant of the given parent
<a name="line166"></a> *     node.
<a name="line167"></a> * @private
<a name="line168"></a> */
<a name="line169"></a>goog.testing.editor.dom.getRelativeDepth_ = function(node, parentNode) {
<a name="line170"></a>  var depth = 0;
<a name="line171"></a>  while (node) {
<a name="line172"></a>    if (node == parentNode) {
<a name="line173"></a>      return depth;
<a name="line174"></a>    }
<a name="line175"></a>    node = node.parentNode;
<a name="line176"></a>    depth++;
<a name="line177"></a>  }
<a name="line178"></a>  return -1;
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Assert that the range is surrounded by the given strings. This is useful
<a name="line184"></a> * because different browsers can place the range endpoints inside different
<a name="line185"></a> * nodes even when visually the range looks the same. Also, there may be empty
<a name="line186"></a> * text nodes in the way (again depending on the browser) making it difficult to
<a name="line187"></a> * use assertRangeEquals.
<a name="line188"></a> * @param {string} before String that should occur immediately before the start
<a name="line189"></a> *     point of the range. If this is the empty string, assert will only succeed
<a name="line190"></a> *     if there is no text before the start point of the range.
<a name="line191"></a> * @param {string} after String that should occur immediately after the end
<a name="line192"></a> *     point of the range. If this is the empty string, assert will only succeed
<a name="line193"></a> *     if there is no text after the end point of the range.
<a name="line194"></a> * @param {goog.dom.AbstractRange} range The range to be tested.
<a name="line195"></a> * @param {Node=} opt_stopAt Node to stop searching at (search will be
<a name="line196"></a> *     restricted to this node&#39;s subtree).
<a name="line197"></a> */
<a name="line198"></a>goog.testing.editor.dom.assertRangeBetweenText = function(before,
<a name="line199"></a>                                                          after,
<a name="line200"></a>                                                          range,
<a name="line201"></a>                                                          opt_stopAt) {
<a name="line202"></a>  var previousText =
<a name="line203"></a>      goog.testing.editor.dom.getTextFollowingRange_(range, true, opt_stopAt);
<a name="line204"></a>  if (before == &#39;&#39;) {
<a name="line205"></a>    assertNull(&#39;Expected nothing before range but found &lt;&#39; + previousText + &#39;&gt;&#39;,
<a name="line206"></a>               previousText);
<a name="line207"></a>  } else {
<a name="line208"></a>    assertNotNull(&#39;Expected &lt;&#39; + before + &#39;&gt; before range but found nothing&#39;,
<a name="line209"></a>                  previousText);
<a name="line210"></a>    assertTrue(&#39;Expected &lt;&#39; + before + &#39;&gt; before range but found &lt;&#39; +
<a name="line211"></a>               previousText + &#39;&gt;&#39;,
<a name="line212"></a>               goog.string.endsWith(
<a name="line213"></a>                   /** @type {string} */ (previousText), before));
<a name="line214"></a>  }
<a name="line215"></a>  var nextText =
<a name="line216"></a>      goog.testing.editor.dom.getTextFollowingRange_(range, false, opt_stopAt);
<a name="line217"></a>  if (after == &#39;&#39;) {
<a name="line218"></a>    assertNull(&#39;Expected nothing after range but found &lt;&#39; + nextText + &#39;&gt;&#39;,
<a name="line219"></a>               nextText);
<a name="line220"></a>  } else {
<a name="line221"></a>    assertNotNull(&#39;Expected &lt;&#39; + after + &#39;&gt; after range but found nothing&#39;,
<a name="line222"></a>                  nextText);
<a name="line223"></a>    assertTrue(&#39;Expected &lt;&#39; + after + &#39;&gt; after range but found &lt;&#39; +
<a name="line224"></a>               nextText + &#39;&gt;&#39;,
<a name="line225"></a>               goog.string.startsWith(
<a name="line226"></a>                   /** @type {string} */ (nextText), after));
<a name="line227"></a>  }
<a name="line228"></a>};
<a name="line229"></a>
<a name="line230"></a>
<a name="line231"></a>/**
<a name="line232"></a> * Returns the text that follows the given range, where the term &quot;follows&quot; means
<a name="line233"></a> * &quot;comes immediately before the start of the range&quot; if isBefore is true, and
<a name="line234"></a> * &quot;comes immediately after the end of the range&quot; if isBefore is false, or null
<a name="line235"></a> * if no non-empty text node is found.
<a name="line236"></a> * @param {goog.dom.AbstractRange} range The range to search from.
<a name="line237"></a> * @param {boolean} isBefore Whether to search before the range instead of
<a name="line238"></a> *     after it.
<a name="line239"></a> * @param {Node=} opt_stopAt Node to stop searching at (search will be
<a name="line240"></a> *     restricted to this node&#39;s subtree).
<a name="line241"></a> * @return {?string} The text that follows the given range, or null if no
<a name="line242"></a> *     non-empty text node is found.
<a name="line243"></a> * @private
<a name="line244"></a> */
<a name="line245"></a>goog.testing.editor.dom.getTextFollowingRange_ = function(range,
<a name="line246"></a>                                                          isBefore,
<a name="line247"></a>                                                          opt_stopAt) {
<a name="line248"></a>  var followingTextNode;
<a name="line249"></a>  var endpointNode = isBefore ? range.getStartNode() : range.getEndNode();
<a name="line250"></a>  var endpointOffset = isBefore ? range.getStartOffset() : range.getEndOffset();
<a name="line251"></a>  var getFollowingTextNode =
<a name="line252"></a>      isBefore ? goog.testing.editor.dom.getPreviousNonEmptyTextNode :
<a name="line253"></a>                 goog.testing.editor.dom.getNextNonEmptyTextNode;
<a name="line254"></a>
<a name="line255"></a>  if (endpointNode.nodeType == goog.dom.NodeType.TEXT) {
<a name="line256"></a>    // Range endpoint is in a text node.
<a name="line257"></a>    var endText = endpointNode.nodeValue;
<a name="line258"></a>    if (isBefore ? endpointOffset &gt; 0 : endpointOffset &lt; endText.length) {
<a name="line259"></a>      // There is text in this node following the endpoint so return the portion
<a name="line260"></a>      // that follows the endpoint.
<a name="line261"></a>      return isBefore ? endText.substr(0, endpointOffset) :
<a name="line262"></a>                        endText.substr(endpointOffset);
<a name="line263"></a>    } else {
<a name="line264"></a>      // There is no text following the endpoint so look for the follwing text
<a name="line265"></a>      // node.
<a name="line266"></a>      followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt);
<a name="line267"></a>      return followingTextNode &amp;&amp; followingTextNode.nodeValue;
<a name="line268"></a>    }
<a name="line269"></a>  } else {
<a name="line270"></a>    // Range endpoint is in an element node.
<a name="line271"></a>    var numChildren = endpointNode.childNodes.length;
<a name="line272"></a>    if (isBefore ? endpointOffset &gt; 0 : endpointOffset &lt; numChildren) {
<a name="line273"></a>      // There is at least one child following the endpoint.
<a name="line274"></a>      var followingChild =
<a name="line275"></a>          endpointNode.childNodes[isBefore ? endpointOffset - 1 :
<a name="line276"></a>                                             endpointOffset];
<a name="line277"></a>      if (goog.testing.editor.dom.isNonEmptyTextNode_(followingChild)) {
<a name="line278"></a>        // The following child has text so return that.
<a name="line279"></a>        return followingChild.nodeValue;
<a name="line280"></a>      } else {
<a name="line281"></a>        // The following child has no text so look for the following text node.
<a name="line282"></a>        followingTextNode = getFollowingTextNode(followingChild, opt_stopAt);
<a name="line283"></a>        return followingTextNode &amp;&amp; followingTextNode.nodeValue;
<a name="line284"></a>      }
<a name="line285"></a>    } else {
<a name="line286"></a>      // There is no child following the endpoint, so search from the endpoint
<a name="line287"></a>      // node, but don&#39;t search its children because they are not following the
<a name="line288"></a>      // endpoint!
<a name="line289"></a>      followingTextNode = getFollowingTextNode(endpointNode, opt_stopAt, true);
<a name="line290"></a>      return followingTextNode &amp;&amp; followingTextNode.nodeValue;
<a name="line291"></a>    }
<a name="line292"></a>  }
<a name="line293"></a>};
</pre>


</body>
</html>
